#include "stdafx.h"
#include "../include/UMyChart9.h"

const POINT INVAILD_POINT={-1,-1};

ChartCorssCursor::ChartCorssCursor()
:m_hPen(NULL)
{
	memcpy(&m_ptX[0],&INVAILD_POINT,sizeof(POINT));
	memcpy(&m_ptX[1],&INVAILD_POINT,sizeof(POINT));

	memcpy(&m_ptY[0],&INVAILD_POINT,sizeof(POINT));
	memcpy(&m_ptY[1],&INVAILD_POINT,sizeof(POINT));
}

ChartCorssCursor::~ChartCorssCursor()
{
}

void ChartCorssCursor::SetPen(HPEN hPen)
{
	m_hPen=hPen;
}

void ChartCorssCursor::InvalidPt()
{
	memcpy(&m_ptX[0],&INVAILD_POINT,sizeof(POINT));
	memcpy(&m_ptX[1],&INVAILD_POINT,sizeof(POINT));

	memcpy(&m_ptY[0],&INVAILD_POINT,sizeof(POINT));
	memcpy(&m_ptY[1],&INVAILD_POINT,sizeof(POINT));
}

void ChartCorssCursor::Draw(UMyChart9& chart,LPPOINT pPtX1,LPPOINT pPtX2,LPPOINT pPtY1,LPPOINT pPtY2,BOOL bErase)
{
	HDC hDC=::GetDC(chart.m_hWnd);
	::BitBlt(hDC,0,0,chart._wndWidth,chart._wndHeight,chart.m_hDC,0,0,SRCCOPY);
	Draw(hDC,pPtX1,pPtX2,pPtY1,pPtY2,FALSE);
	::ReleaseDC(chart.m_hWnd,hDC);
}

void ChartCorssCursor::DrawOldCorssCursor(HDC hDC)
{
	return;
	int nOldROP=GetROP2(hDC);
	SetROP2(hDC,R2_XORPEN);
	HGDIOBJ hOldObj=SelectObject(hDC,m_hPen);

	if (m_ptX[0].x!=-1 && m_ptX[0].y!=-1 && 
		m_ptX[1].x!=-1 && m_ptX[1].y!=-1 )
	{
		MoveToEx(hDC,m_ptX[0].x,m_ptX[0].y,NULL);
		LineTo(hDC,m_ptX[1].x,m_ptX[1].y);
	}

	if (m_ptY[0].x!=-1 && m_ptY[0].y!=-1 && 
		m_ptY[1].x!=-1 && m_ptY[1].y!=-1)
	{
		MoveToEx(hDC,m_ptY[0].x,m_ptY[0].y,NULL);
		LineTo(hDC,m_ptY[1].x,m_ptY[1].y);
	}

	SelectObject(hDC,hOldObj);
	SetROP2(hDC,nOldROP);
}

void ChartCorssCursor::DrawOldCorssCursor(UMyChart9& chart)
{
	HDC hDC=::GetDC(chart.m_hWnd);
	DrawOldCorssCursor(hDC);
	
	::ReleaseDC(chart.m_hWnd,hDC);
}

void ChartCorssCursor::ClearOldPoint()
{
	//LogTrace(LOG_LEVEL_NORMAL,_T("ClearOldPoint"));

	memcpy(&m_ptX[0],&INVAILD_POINT,sizeof(POINT));
	memcpy(&m_ptX[1],&INVAILD_POINT,sizeof(POINT));

	memcpy(&m_ptY[0],&INVAILD_POINT,sizeof(POINT));
	memcpy(&m_ptY[1],&INVAILD_POINT,sizeof(POINT));
}

void ChartCorssCursor::Draw(HDC hDC,LPPOINT pPtX1,LPPOINT pPtX2,LPPOINT pPtY1,LPPOINT pPtY2,BOOL bErase)
{
	if (m_hPen==NULL) return;

	int nOldROP=GetROP2(hDC);
	SetROP2(hDC,R2_XORPEN);
	//CPen pen(PS_DOT,0,RGB(255,255,255));
	HGDIOBJ hOldObj=SelectObject(hDC,m_hPen);
	//HGDIOBJ hOldObj=SelectObject(hDC,pen);
	
	//LogTrace(LOG_LEVEL_NORMAL,_T("2.[%d,%d],[%d,%d]   [%d,%d] [%d,%d]"),
	//		m_ptX[0].x,m_ptX[0].y,m_ptX[1].x,m_ptX[1].y,
	//		m_ptY[0].x,m_ptY[0].y,m_ptY[1].x,m_ptY[1].y);

	if (bErase)
	{
		if (m_ptX[0].x!=-1 && m_ptX[0].y!=-1 && 
			m_ptX[1].x!=-1 && m_ptX[1].y!=-1 )
		{
			MoveToEx(hDC,m_ptX[0].x,m_ptX[0].y,NULL);
			LineTo(hDC,m_ptX[1].x,m_ptX[1].y);
		}

		if (m_ptY[0].x!=-1 && m_ptY[0].y!=-1 && 
			m_ptY[1].x!=-1 && m_ptY[1].y!=-1)
		{
			MoveToEx(hDC,m_ptY[0].x,m_ptY[0].y,NULL);
			LineTo(hDC,m_ptY[1].x,m_ptY[1].y);
		}
	}

	if (pPtX1 && pPtX2)
	{
		MoveToEx(hDC,pPtX1->x,pPtX1->y,NULL);
		LineTo(hDC,pPtX2->x,pPtX2->y);

		memcpy(&m_ptX[0],pPtX1,sizeof(POINT));
		memcpy(&m_ptX[1],pPtX2,sizeof(POINT));
	}
	else
	{
		memcpy(&m_ptX[0],&INVAILD_POINT,sizeof(POINT));
		memcpy(&m_ptX[1],&INVAILD_POINT,sizeof(POINT));
	}

	if (pPtY1 && pPtY2)
	{
		MoveToEx(hDC,pPtY1->x,pPtY1->y,NULL);
		LineTo(hDC,pPtY2->x,pPtY2->y);

		memcpy(&m_ptY[0],pPtY1,sizeof(POINT));
		memcpy(&m_ptY[1],pPtY2,sizeof(POINT));
	}
	else
	{
		memcpy(&m_ptY[0],&INVAILD_POINT,sizeof(POINT));
		memcpy(&m_ptY[1],&INVAILD_POINT,sizeof(POINT));
	}

	//LogTrace(LOG_LEVEL_NORMAL,_T("3.[%d,%d],[%d,%d]   [%d,%d] [%d,%d] \r\n"),
	//		m_ptX[0].x,m_ptX[0].y,m_ptX[1].x,m_ptX[1].y,
	//		m_ptY[0].x,m_ptY[0].y,m_ptY[1].x,m_ptY[1].y);

	SelectObject(hDC,hOldObj);
	SetROP2(hDC,nOldROP);
}